package com.shanghaionstar.activity.webview.handler;

import android.content.Context;
import android.os.Build;
import android.os.Message;
import android.util.DisplayMetrics;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * Created by Rengui Lu on 2017/3/3.
 */
public class RemoteControlWebHandler extends DefaultWebHandler {

    public RemoteControlWebHandler(Context context) {
        super(context);
    }

    @Override
    public void handMsg(Message msg) {
        switch (msg.what) {
            case IWebType.TYPE_ON_PAGE_STARTED:
                setRemoteControlSetting(msg);
                break;
        }
    }

    private void setRemoteControlSetting(Message msg) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            WebView webView = mActivity.getWebview();
            String url = (String) msg.obj;
            setRemoteControlSetting(webView, url);
        }
    }

    public void setRemoteControlSetting(WebView webView, String url) {
        if (url.contains("remote_control") && Build.MODEL.contains("GT-N7100")) {
            int dpi = context.getResources().getDisplayMetrics().densityDpi;
            webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
            webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            webView.getSettings().setTextZoom(dpi > DisplayMetrics.DENSITY_XHIGH ? 80 : 90);
        }
    }
}
